package com.wellsoft.pt.app.dingtalk.job;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.quartz.JobExecutionContext;

import com.wellsoft.context.util.ApplicationContextHolder;
import com.wellsoft.pt.app.dingtalk.entity.EventCallBack;
import com.wellsoft.pt.app.dingtalk.service.EventCallBackService;
import com.wellsoft.pt.app.dingtalk.service.MultiOrgDingDeptService;
import com.wellsoft.pt.app.dingtalk.service.MultiOrgDingUserInfoService;
import com.wellsoft.pt.task.job.Job;
import com.wellsoft.pt.task.job.JobData;

import net.sf.json.JSONArray;

/**
 * Description: 定时钉钉事件回调任务
 * 钉钉事件回调，解析结果数据，落地数据到对应数据表
 *  
 * @author Well
 * @date 2020年5月29日
 * @version 1.0
 * 
 * <pre>
 * 修改记录:
 * 修改后版本	修改人		修改日期			修改内容
 * 2020年5月29日.1	Well		2020年5月29日		Create
 * </pre>
 *
 */
public class DingtalkEventCallBackJob extends Job {

	@Override
	protected void execute(JobExecutionContext arg0, JobData arg1) {
		EventCallBackService eventCallBackService = ApplicationContextHolder.getBean(EventCallBackService.class);
		List<EventCallBack> listEventCallBack = eventCallBackService.listEventCallBack();
		if (CollectionUtils.isEmpty(listEventCallBack))
			return;
		MultiOrgDingUserInfoService multiOrgDingUserInfoService = ApplicationContextHolder
				.getBean(MultiOrgDingUserInfoService.class);
		MultiOrgDingDeptService multiOrgDingDeptService = ApplicationContextHolder
				.getBean(MultiOrgDingDeptService.class);
		for (EventCallBack event : listEventCallBack) {
			String eventType = event.getEventType();
			switch (eventType) {
			case "user_add_org":// 通讯录用户增加 do something
				JSONArray userIds = JSONArray.fromObject(event.getDingUserId());
				multiOrgDingUserInfoService.saveUserFromDingtalkEventCallBack(userIds, eventType);
				break;
			case "user_modify_org":// 通讯录用户更改 do something
				userIds = JSONArray.fromObject(event.getDingUserId());
				multiOrgDingUserInfoService.updateUserFromDingtalkEventCallBack(userIds);
				break;
			case "user_leave_org":// 通讯录用户离职 do something
				userIds = JSONArray.fromObject(event.getDingUserId());
				multiOrgDingUserInfoService.deleteUserFromDingtalkEventCallBack(userIds);
				break;
			case "org_admin_add":// 通讯录用户被设为管理员 do something
				break;
			case "org_admin_remove":// 通讯录用户被取消设置管理员 do something
				break;
			case "org_dept_create":// 通讯录企业部门创建
				JSONArray deptIds = JSONArray.fromObject(event.getDingDeptId());
				multiOrgDingDeptService.saveDeptFromDingtalkEventCallBack(deptIds, eventType);
				break;
			case "org_dept_modify":// 通讯录企业部门修改
				deptIds = JSONArray.fromObject(event.getDingDeptId());
				multiOrgDingDeptService.saveDeptFromDingtalkEventCallBack(deptIds, eventType);
				break;
			case "org_dept_remove":// 通讯录企业部门删除
				deptIds = JSONArray.fromObject(event.getDingDeptId());
				multiOrgDingDeptService.saveDeptFromDingtalkEventCallBack(deptIds, eventType);
				break;
			case "org_remove":// 企业被解散 do something
				break;
			case "check_url":// do something
				break;
			case "bpms_task_change":
				break;
			case "bpms_instance_change":
				break;
			default: // do something
				break;
			}
			event.setStatus(1);
			eventCallBackService.update(event);
		}
	}
}
